﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块的字段类型控件，图片上传控件
 * 
 * 修改人员：yylu
 * 
 * 修改日期：2009/08/20
 * 
 * 修改记录：1)增加控件手写单独使用的代码
 *           2) 增加控件上传文件的格式(不区分大小写),大小,并给出提示 yhe 2009-09-10
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;
using Whir.Common;
using System.IO;

public partial class UserControls_FieldControl_PictureType : Whir.Controls.ModelField.BaseFieldControl
{
    protected int maxSize = 0;
    protected string allowType = "";
    protected int ItemID;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region 注册magicthumb相关文件

        //string jsUrl = Whir.Common.Settings.GetString("SystemPath") + "Script/magicthumb-packed.js";
        //string cssUrl = Whir.Common.Settings.GetString("SystemPath") + "Script/magicthumb.css";
        //HtmlLink link = new HtmlLink();
        //link.Attributes.Add("type", "text/css");
        //link.Attributes.Add("rel", "stylesheet");
        //link.Attributes.Add("href", cssUrl);//url为css路径
        //Page.Header.Controls.Add(link);

        //Page.ClientScript.RegisterClientScriptInclude("magicthumb", jsUrl);


        #endregion

        Whir.BLL.Whir_FieldUpload bll = new Whir.BLL.Whir_FieldUpload();
        int fieldTextId = int.Parse(bll.GetList(" FieldID = " + this.FieldID).Tables[0].Rows[0][0].ToString());
        Whir.Model.Whir_FieldUpload model = bll.GetModel(fieldTextId);

        maxSize = model.maxSize;
        allowType = model.allowType;

        if (this.isSingleUse)
        {
            this.InitFieldControl(this.FieldID);
        }

        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(this.FieldValue))
            {
                ItemID = NetRequest.GetQueryInt("ItemID");
                if (ItemID == 0)
                {
                    //添加操作，绑定默认值
                    this.txtPictureUrl.Value = model.defaultValue;
                    this.lbDelete.Visible = false;
                    this.lbReSubmit.Visible = false;
                    this.hyUrl.Visible = false;
                }
                else
                {
                    setValue(FieldCommon.GetBindValue(this.FieldID, ItemID));                    
                }
            }
            else
            {
                setValue(this.FieldValue);
            }
        }
        else
        {
            this.FieldValue = this.txtPictureUrl.Visible ? this.txtPictureUrl.Value : this.lblPictureUrl.Text;            
        }
    }

    private void setValue(string sValue)
    {
        //更新操作，绑定值
        lblPictureUrl.Text = txtPictureUrl.Value = sValue;// FieldCommon.GetBindValue(this.FieldID, ItemID);
        txtPictureUrl.Visible = true;

        #region 图片放大处理
        //hyUrl.NavigateUrl = "~/UploadFiles/image/" + this.txtPictureUrl.Value;
        //hyUrl.Visible = true;

        MagicThumb_A.HRef = "~/UploadFiles/image/" + this.txtPictureUrl.Value;
        MagicThumb_IMG.Src = "~/UploadFiles/image/" + this.txtPictureUrl.Value;

        //读取图片实际大小                
        string imgSrc = Server.MapPath(MagicThumb_IMG.Src);
        if (!string.IsNullOrEmpty(this.txtPictureUrl.Value))
        {
            if (File.Exists(imgSrc))
            {
                System.Drawing.Image MyImage = System.Drawing.Image.FromFile(imgSrc);
                ChangeImg(MagicThumb_IMG, MyImage.Width, MyImage.Height);
                MyImage.Dispose();
            }
        }

        hyUrl.Visible = true;
        #endregion

        ezwPictureType.Visible = false;
        btnUpload.Visible = false;

        if (string.IsNullOrEmpty(txtPictureUrl.Value))
        {
            txtPictureUrl.Visible = true;
            ezwPictureType.Visible = true;
            btnUpload.Visible = true;
            lbReSubmit.Visible = false;
            hyUrl.Visible = false;
            lbDelete.Visible = false;
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!this.ezwPictureType.HasFile)
        {
            JsHelper.Alert(Page, "请选择要上传的图片!");
            return;
        }

        //判断上传文件是否符合格式, 不区分大小写
        string fileExt = System.IO.Path.GetExtension(this.ezwPictureType.FileName).Remove(0, 1).ToLower();
        if (("|" + allowType + "|").IndexOf(("|" + fileExt + "|")) < 0)
        {
            JsHelper.Alert(Page, "上传文件格式错误！允许上传文件类型（" + allowType + "）,不区分大小写!");
            return;
        }


        //判断图片的大小是否超过
        if (this.ezwPictureType.FileBytes.Length > maxSize * 1000)
        {
            JsHelper.Alert(Page, "上传文件大小错误！最大允许上传 " + maxSize + " KB");
            return;
        }


        FileUploadHelper upload = new FileUploadHelper();

        upload.UploadDir = "~/UploadFiles/image/";
        upload.UploadFileExt = allowType;
        upload.UploadFileSize = maxSize;
        upload.IsUseRandFileName = true;
        upload.Upload(this.ezwPictureType);

        this.lblPictureUrl.Text = this.txtPictureUrl.Value = Path.GetFileName(upload.UploadPath);

        #region 图片放大处理
        //hyUrl.NavigateUrl = upload.UploadPath;

        hyUrl.Visible = true;
        MagicThumb_A.HRef = "~/UploadFiles/image/" + this.txtPictureUrl.Value;
        MagicThumb_IMG.Src = "~/UploadFiles/image/" + this.txtPictureUrl.Value;

        //读取图片实际大小                
        string imgSrc = Server.MapPath(MagicThumb_IMG.Src);
        if (!string.IsNullOrEmpty(this.txtPictureUrl.Value))
        {
            if (File.Exists(imgSrc))
            {
                System.Drawing.Image MyImage = System.Drawing.Image.FromFile(imgSrc);
                ChangeImg(MagicThumb_IMG, MyImage.Width, MyImage.Height);
                MyImage.Dispose();
            }
        }

        #endregion

        if (upload.UploadResult == "文件上传成功！")
        {
            ezwPictureType.Visible = false;
            btnUpload.Visible = false;
            lbReSubmit.Visible = true;
            lbDelete.Visible = true;
        }
        //ltMessage.Text = upload.UploadResult;
    }

    protected void lbReSubmit_Click(object sender, EventArgs e)
    {
        txtPictureUrl.Visible = true;
        lblPictureUrl.Visible = false;
        ezwPictureType.Visible = true;
        btnUpload.Visible = true;
        lbReSubmit.Visible = false;
        hyUrl.Visible = false;
        lbDelete.Visible = false;
    }

    protected void lbDelete_Click(object sender, EventArgs e)
    {
        string file = Server.MapPath("~/UploadFiles/image/") + this.lblPictureUrl.Text;
        if (File.Exists(file))
        {
            File.Delete(file);
            JsHelper.Alert("文件已删除！");
            this.lblPictureUrl.Text = "";
            this.txtPictureUrl.Value = "";
            txtPictureUrl.Visible = true;
            ezwPictureType.Visible = true;
            btnUpload.Visible = true;
            lbReSubmit.Visible = false;
            hyUrl.Visible = false;
            lbDelete.Visible = false;
        }
        else
            JsHelper.Alert("文件不存在！");
    }

    /// <summary>
    /// 按比例缩小图片
    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    private void ChangeImg(HtmlImage imgObj,int Width,int Height)
    {
        double width = 200, height = 200;
        double size = 100;
        double newWidth, newHeight;        

        if (Width > width)
        {
            size = double.Parse(Width.ToString()) / width;
            newWidth = System.Math.Round(Width / size);
            newHeight = System.Math.Round(Height / size);
            imgObj.Width = int.Parse(newWidth.ToString());
            imgObj.Height = int.Parse(newHeight.ToString());
        }
        else if (Height > height)
        {
            size = double.Parse(Height.ToString()) / height;
            newWidth = System.Math.Round(Width / size);
            newHeight = System.Math.Round(Height / size);
            imgObj.Width = int.Parse(newWidth.ToString());
            imgObj.Height = int.Parse(newHeight.ToString());
        }
    }
}
